import payMentService from  '../services/payMentService';
import { Toast ,Modal} from 'antd-mobile'
const alert = Modal.alert;

export const payMent =(data,callBack)=>{
	return (dispatch, getState)=> {
		Toast.hide();
		Toast.loading("", 30, () => Toast.info("网络错误", 2));
		//2 支付宝  3微信
		payMentService.payMent(data).then(resultData => {
			Toast.hide();
			// document.write(resultData.body.alipayBizSubmitText)
			// alert(resultData)
			if (resultData.code == 1000) {
				callBack(resultData.body.chargeorderId)
				if(data.payType ==2){
					checkOutCounterPay(resultData.body.chargeorderId)
				}else if(data.payType ==3){
					weixinPay(resultData.body.chargeorderId)
				}
				// location.href=resultData.paySign;
			} else {
				Toast.info(resultData.msg);
			}
		});
	}
}

/*
*   支付宝支付
* */
export const checkOutCounterPay =(data,callBack)=>{
		let postDat={
			chargeOrderId: data
		}
		Toast.hide();
		Toast.loading("", 30, () => Toast.info("网络错误", 2));
		payMentService.aliPay(postDat).then(resultData => {
			 Toast.hide();
			if (resultData.code == 1000) {
			} else {
				Toast.info(resultData.msg);
			}
		});
}

/*
 *   微信支付
 * */
export const weixinPay =(data,callBack)=>{
	let postDat={
		chargeOrderId: data
	}
	Toast.hide();
	Toast.loading("", 30, () => Toast.info("网络错误", 2));
	payMentService.weixinPay(postDat).then(resultData => {
		Toast.hide();
		if (resultData.code == 1000) {
			sessionStorage.setItem('weixinPay',1)
			sessionStorage.setItem('forwardUrl',resultData.body.forwardUrl)
			sessionStorage.setItem('wxData',data)
			location.href = resultData.body.wxPayUrl;
		} else {
			Toast.info(resultData.msg);
		}
	});
}

/*
 *   微信查询
 *   SUCCESS—支付成功
		 REFUND—转入退款
		 NOTPAY—未支付
		 CLOSED—已关闭
		 REVOKED—已撤销（刷卡支付）
		 USERPAYING--用户支付中
      PAYERROR--支付失败(其他原因，如银行返回失败)
 * */
export const weixinQuery =(data,successUrl,callBack)=>{
	// console.log('查询',data,successUrl)

	let postDat={
		chargeOrderId: data
	}

	Toast.hide();
	Toast.loading("", 30, () => Toast.info("网络错误", 2));
	payMentService.weixinQuery(postDat).then(resultData => {
		Toast.hide();
		if (resultData.code == 1000) {  //跳到支付成功页
			location.href = successUrl
		} else {
			Toast.info(resultData.msg);
		}
	});
}